package m3;

import com.rabbitmq.client.*;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import java.util.UUID;

/*
    2022/1/20
    15:40
*/public class Consumer {
    public static void main(String[] args) throws Exception{
        //1.连接服务器
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.64.140");
        factory.setPort(5672);
        factory.setUsername("admin");
        factory.setPassword("admin");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        //2.创建队列，创建交换机，进行绑定
        String queue = UUID.randomUUID().toString();
        //2.1声明队列
        channel.queueDeclare(queue,false,true,true,null);
        //2.2声明交换机
        channel.exchangeDeclare("logs","fanout");
        //2.3绑定交换机与队列，对 fanout 交换机，第三个参数无效
        channel.queueBind(queue,"logs","");

        //3.接收消息
        DeliverCallback deliverCallback = (consumerTag, message) -> {
            String s = new String(message.getBody(), "UTF-8");
            System.out.println("收到:"+s);
        };
        CancelCallback cancelCallback = consumerTag -> {};

        channel.basicConsume(queue,true,deliverCallback,cancelCallback);

    }
}
